package com.connector.server.server;

import org.slf4j.LoggerFactory;
import com.connector.server.server.listener.SocketIoHandshakeCheckListener;
import com.connector.base.message.model.HandshakeCheckMessage;
import com.connector.server.server.listener.SocketIoChatListener;
import com.connector.base.message.model.ChatMessage;
import com.corundumstudio.socketio.listener.DataListener;
import com.connector.server.server.listener.SocketIoHandshakeListener;
import com.connector.base.message.model.HandshakeMessage;
import com.corundumstudio.socketio.listener.DisconnectListener;
import com.connector.server.server.listener.SocketIoDisconnectListener;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.connector.server.server.listener.SocketIoConnectListener;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.Transport;
import com.corundumstudio.socketio.listener.ExceptionListener;
import com.connector.base.exception.CutExceptionListener;
import com.connector.base.exception.ServerException;
import com.connector.base.config.InstantConfig;
import com.connector.server.InstantServer;
import com.connector.base.connection.ConnectionRegistry;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.Configuration;
import org.slf4j.Logger;
import com.connector.base.service.IServer;
import com.connector.base.service.BaseService;

public class ConnectorSocketIOServer extends BaseService implements IServer
{
    private static final Logger log;
    private Configuration config;
    private SocketIOServer server;
    private final String host;
    private final int port;
    private final String origin;
    private final ConnectionRegistry connectionRegistry;
    private final InstantServer instantServer;

    public ConnectorSocketIOServer(final InstantServer instantServer) {
        this.connectionRegistry = instantServer.getConnectionRegistry();
        this.instantServer = instantServer;
        this.host = InstantConfig.server.host;
        this.port = InstantConfig.server.socketio.port;
        this.origin = InstantConfig.server.socketio.origin;
    }

    public void init() {
        if (this.started.get()) {
            ConnectorSocketIOServer.log.error("server already started");
            throw new ServerException("server already started");
        }
        this.config = new Configuration();
    }

    public void start() {
        this.init();
        this.config.setPort(this.port);
        this.config.setExceptionListener((ExceptionListener)new CutExceptionListener());
        this.config.setTransports(new Transport[] { Transport.POLLING, Transport.WEBSOCKET });
        this.config.setAuthorizationListener((AuthorizationListener)new AuthorizationListener() {
            public boolean isAuthorized(final HandshakeData handshakeData) {
                return true;
            }
        });
        ConnectorSocketIOServer.log.info("server config {}", (Object)this.config);
        this.server = new SocketIOServer(this.config);
        ConnectorSocketIOServer.log.info("begin create listeners");
        this.server.addConnectListener(new SocketIoConnectListener(this.connectionRegistry));
        this.server.addDisconnectListener(new SocketIoDisconnectListener(this.connectionRegistry));
        this.server.addEventListener("_handshakeEvent", (Class)HandshakeMessage.class, (DataListener)new SocketIoHandshakeListener(this.connectionRegistry));
        this.server.addEventListener("message", (Class)ChatMessage.class, (DataListener)new SocketIoChatListener(this.connectionRegistry));
        this.server.addEventListener("_handshakeCheck", (Class)HandshakeCheckMessage.class, (DataListener)new SocketIoHandshakeCheckListener(this.connectionRegistry));
        ConnectorSocketIOServer.log.info("after create listeners,begin start");
        this.server.start();
        ConnectorSocketIOServer.log.info("socketio server started at port {}", (Object)this.config.getPort());
    }

    public void stop() {
        this.server.stop();
    }

    static {
        log = LoggerFactory.getLogger((Class)ConnectorSocketIOServer.class);
    }
}
